package org.ee.core;

import io.netty.buffer.ByteBuf;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import java.util.ArrayList;
import java.util.List;

/**
 * 协议注册
 * @Author zlj
 * @Date 2025/11/8 23:57
 */
public class ProtocolRegistry {
    private static final Logger logger = LogManager.getLogger(ProtocolRegistry.class);

    private static final List<ProtocolDefinition> protocols = new ArrayList<>();

    public static void register(ProtocolDefinition protocol) {
        protocols.add(protocol);
        logger.info("Registered protocol: " + protocol.getName());
    }

    public static void unregister(ProtocolDefinition protocol) {
        protocols.remove(protocol);
       logger.info("Unregistered protocol: " + protocol.getName());
    }

    public static ProtocolDefinition detect(ByteBuf in) {
        for (ProtocolDefinition p : protocols) {
            if (p.matches(in)) {
                return p;
            }
        }
        return null;
    }
}
